<template>
<div>
  <el-select v-model="libraryId" @change="change" placeholder="请选择产品库" size="small">
    <el-option
      v-for="item in libraryOptions"
      :key="item.libraryId"
      :label="item.name"
      :value="item.libraryId"
    />
  </el-select>
</div>
</template>

<script>
import {selectLibrary} from "@/api/mall/product/library";

export default {
  name: "LibrarySelect",
  props: {
    refresh: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      // 产品库字典
      libraryOptions: [],
      libraryId: null,
      queryParams:{
        libraryId: null
      }
    }
  },
  created() {
    this.getSelect();
  },
  methods:{
    getSelect(){
      this.queryParams.libraryId = this.$store.state.settings.libraryId;
      selectLibrary(this.queryParams).then(response => {
        this.libraryOptions = response.data;
        if(this.libraryOptions.length>0){
          this.libraryId = this.libraryOptions[0].libraryId;
          this.updateLibraryId(this.libraryId);
        }
      });
    },
    updateLibraryId(val){
      this.$store.dispatch('settings/changeSetting', {
        key: 'libraryId',
        value: val
      });
      localStorage.setItem(
        "library-setting",
        `{
            "libraryId":${val}
          }`
      );

    },
    change(value){
      this.updateLibraryId(value);
      this.refresh();
    }
  }
}
</script>

<style scoped>

</style>
